#include "rollingaudioinputdevice.h"

RollingAudioInputDevice::RollingAudioInputDevice(QObject*parent):QIODevice(parent)
{
}
qint64 RollingAudioInputDevice::readData(char*data,qint64 maxSize)
{
    Q_UNUSED(data)
    Q_UNUSED(maxSize)
    return -1;
}
qint64 RollingAudioInputDevice::writeData(const char*data,qint64 maxSize)
{
    static const int resolution = 1;
    if (m_buffer.isEmpty()) {
        m_buffer.reserve(sampleCount);
        m_fbuffer.reserve(sampleCount);
        for (int i = 0; i < sampleCount; ++i)
        {
            m_buffer.append(0);
            m_fbuffer.append(0);
        }
    }
    int start = 0;
    //const int availableSamples = int(maxSize) / resolution;
    const int availableSamples = int(maxSize) / resolution / 2;
    if (availableSamples < sampleCount) {
        start = sampleCount - availableSamples;
        for (int s = 0; s < start; ++s)
        {
            m_buffer[s]=m_buffer.at(s + availableSamples);
            m_fbuffer[s]=m_fbuffer.at(s+availableSamples);
        }
    }

    for (int s = start; s < sampleCount; ++s, data += resolution*2)
    {
        m_buffer[s]=(*data)+((*(data+1))<<8);
        m_fbuffer[s]=m_buffer[s]/1.0;
    }
    return (sampleCount - start) * resolution*2;
}
